package top.zosoft.builder;

import io.netty.util.NettyRuntime;

import org.springframework.beans.factory.annotation.Autowired;
import top.zosoft.enums.ServerType;
import top.zosoft.server.MqttBroker;
import top.zosoft.server.Server;

/**
 * @author neal
 * @date 2023/12/21  11:44
 */
public class ServerBuilder {
    //服务端
    public Server server;
    //服务端名称
    public String name;
    //服务端类型
    public final ServerType type;

    //boss线程核心数
    public final int bossCore;
    //工作线程核心数
    public final int workerCore;
    //写空闲时间
    public final int writerIdleTime;
    //读空闲时间
    public final int readerIdleTime;
    //读写空闲时间
    public final int allIdleTime;
    //端口
    public final int port;

    public ServerBuilder(Server server, String name, ServerType type,int bossCore, int workerCore, int writerIdleTime, int readIdleTime, int allIdleTime,int port) {
        this.server = server;
        this.name = name;
        this.type = type;
        this.bossCore = bossCore;
        this.workerCore = workerCore;
        this.writerIdleTime = writerIdleTime;
        this.readerIdleTime = readIdleTime;
        this.allIdleTime = allIdleTime;
        this.port = port;
        switch (type){
            case MQTT:
                this.server = server;
                this.server.config = this;
                break;
            case TCP:
                break;
            case UDP:
                break;
            case COAP:
                break;
            case WEBSOCKET:
                break;
            case LWM2M:
                break;
            case GB28181:
                break;
            case OTHER:
                break;
            default:
                break;
        }
    }
    private Server build(){
        return this.server;
    }

    //获取内部类实例
    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private Server server;
        private String name;
        private ServerType type;
        private int bossCore = 1;
        private int workerCore = NettyRuntime.availableProcessors() + 2;
        private int writerIdleTime = 240;
        private int readIdleTime = 0;
        private int allIdleTime = 0;
        private int port;
        public Builder setServer(Server server) {
            this.server = server;
            return this;
        }

        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        public Builder setType(ServerType type) {
            this.type = type;
            return this;
        }

        public Builder setBossCore(int bossCore) {
            this.bossCore = bossCore;
            return this;
        }

        public Builder setWorkerCore(int workerCore) {
            this.workerCore = workerCore;
            return this;
        }

        public Builder setWriterIdleTime(int writerIdleTime) {
            this.writerIdleTime = writerIdleTime;
            return this;
        }

        public Builder setReadIdleTime(int readIdleTime) {
            this.readIdleTime = readIdleTime;
            return this;
        }

        public Builder setAllIdleTime(int allIdleTime) {
            this.allIdleTime = allIdleTime;
            return this;
        }

        public Builder setPort(int port) {
            this.port = port;
            return this;
        }

        public Server build() {
            ServerBuilder serverBuilder = new ServerBuilder(server, name, type, bossCore, workerCore, writerIdleTime, readIdleTime, allIdleTime,port);
            return serverBuilder.build();
        }
    }


}
